package com.example.t1;

import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;

public class Chessboard {
    static int board[][] = new int[11][10];
    static int nandu=10;
    static public String name = "huangkangjian";
    static public int photo = 1;
    Intent intent = null;

    Chessboard(){
        init();
        reset();
    }

    boolean isWin(){
        for(int i=1;i<=9;i++){
            for(int j=1;j<=8;j++){
                if(board[i][j]>0)
                    return false;
            }
        }
        return true;
    }
    public void setNandu(int n){
        nandu=n;
        init();
    }

    Queue<Integer> queue = new LinkedList<Integer>();

    private boolean available(int x,int y){
        if(x<11&&x>=0&&y<10&&y>=0&&board[x][y]==-1){
            return true;
        }
        return false;
    }
    boolean bfs(int x,int y,int a,int b){
        int vis[][]=new int[18][18];
        vis[x][y]=1;
        int tmp;
        if(Math.abs(x-a)==1&&y==b || Math.abs(y-b)==1&&x==a)
            return true;
        if(available(x+1,y)&&vis[x+1][y]==0){
            tmp = (x+1)*10+y;
            queue.offer(tmp);
        }
        if(available(x-1,y)&&vis[x-1][y]==0){
            tmp = (x-1)*10+y;
            queue.offer(tmp);
        }
        if(available(x,y+1)&&vis[x][y+1]==0){
            tmp = (x)*10+y+1;
            queue.offer(tmp);
        }
        if(available(x,y-1)&&vis[x][y-1]==0){
            tmp = (x)*10+y-1;
            queue.offer(tmp);
        }
        while (!queue.isEmpty()){
            int top = queue.poll();
            int xx = top/10;
            int yy = top%10;
            if(Math.abs(xx-a)==1&&yy==b || Math.abs(yy-b)==1&&xx==a){
                Log.i("ss",xx+" "+yy);
                Log.i("ss",a+" "+b);
                return true;
            }
            vis[xx][yy]=1;
            if(available(xx+1,yy)&&vis[xx+1][yy]==0){
                tmp = (xx+1)*10+yy;
                queue.offer(tmp);
            }
            if(available(xx-1,yy)&&vis[xx-1][yy]==0){
                tmp = (xx-1)*10+yy;
                queue.offer(tmp);
            }
            if(available(xx,yy+1)&&vis[xx][yy+1]==0){
                tmp = (xx)*10+yy+1;
                queue.offer(tmp);
            }
            if(available(x,yy-1)&&vis[xx][yy-1]==0){
                tmp = (xx)*10+yy-1;
                queue.offer(tmp);
            }
        }
        return false;
    }

    int getNo(int i,int j){
        return board[i][j];
    }
    static public void init(){
        GameFragment.timeNum = 0;
        for (int i = 0; i < 11; i++) {
            for(int j = 0;j < 10; j++){
                board[i][j]=-1;
            }
        }
        Random rand = new Random();
        for (int i = 1; i <= 9; i++) {
            for(int j = 1;j <= 4; j++){
                int k = rand.nextInt(nandu);
                board[i][j]=k;
                board[i][j+4]=k;
            }
        }
    }
    public void setDis(int x,int y){
        board[x][y]=-1;
    }
    public boolean check(int x1,int y1,int x2,int y2){
        if(x1==x2&&y1==y2){
            return false;
        }
        else if(board[x1][y1]==board[x2][y2] && bfs(x1,y1,x2,y2)){
            return true;
        }
        return false;
    }

    public void reset(){
        for(int i=0;i<50;i++){
            Random rand = new Random();
            int x1 = rand.nextInt(9)+1;
            int x2 = rand.nextInt(9)+1;
            int y1 = rand.nextInt(8)+1;
            int y2 = rand.nextInt(8)+1;
            int t = board[x1][y1];
            board[x1][y1] = board[x2][y2];
            board[x2][y2] = t;
        }
    }
}
